#include <iostream>

#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"


struct State{
    std::string s;
    int step;
    int empty_pos;
    State* last= nullptr;
};
class OneLoop {
public:
    std::list<State> queue;
    std::list<State> out;
    std::unordered_map<std::string,char> appeared;
    bool run(std::string &s1,std::string &s2
    ) {
        State st;
        st.s=s1;
        st.step=0;
        st.empty_pos=st.s.find('*');
        int res=0;
        appeared[s1]=1;
        queue.push_back(st);
        while (!queue.empty()){
            out.splice(out.begin(),queue,queue.begin());
            auto & one=out.front();
            if(one.s==s2){
                res=one.step;
                printf("%d\n",res);
//                State* p=&one;
//                while (p){
//                    std::cout<<p->s<<std::endl;
//                    p=p->last;
//                }
                break;
            }
            int left_side= std::max(0,one.empty_pos-3);
            int right_side=std::min(int(one.s.size()-1),one.empty_pos+3);
            for(int i=left_side;i<one.empty_pos;i++){
                std::swap(one.s[i],one.s[one.empty_pos]);
                if(appeared.find(one.s)==appeared.end()){
                    appeared[one.s]=1;
                    State news=one;
                    news.step++;
                    news.empty_pos=i;
                    news.last=&one;
                    queue.push_back(news);
                }
                std::swap(one.s[i],one.s[one.empty_pos]);
            }
            for(int i=one.empty_pos+1;i<=right_side;i++){
                std::swap(one.s[i],one.s[one.empty_pos]);
                if(appeared.find(one.s)==appeared.end()){
                    appeared[one.s]=1;
                    State news=one;
                    news.step++;
                    news.empty_pos=i;
                    news.last=&one;
                    queue.push_back(news);
                }
                std::swap(one.s[i],one.s[one.empty_pos]);
            }

        }

        ;
        return true;
    }
};

class Solution {
public:
    void run() {

        bool first = true;
        int a, b, c, d, e;
        //        std::cin >> a;
        std::string s1,s2;
        while (std::getline(std::cin,s1)) {
            std::getline(std::cin,s2);
            OneLoop l;
            if (!l.run(s1,s2)) {
                //                return;
            }
        }


    }
};

int main() {
    //    bool cur_num= true;
    Solution().run();
    return 0;
}

